﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BillingSystem.DataSetMainTableAdapters;

namespace BillingSystem
{
    public partial class FormDraft : Form
    {
        DataGridViewTextBoxColumn dURATIONDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn cOSTDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn NUMBERDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();

        DataSetMain dataSet;
        NUMBERS_STATSTableAdapter t_adapter = new NUMBERS_STATSTableAdapter();
        DRAFT_VIEWTableAdapter t_adapter_draft = new DRAFT_VIEWTableAdapter();
        BindingSource bindingSource;
        BindingSource bindingSourceDraft;
        decimal c_id, acc_id;
        
        

        public FormDraft()
        {
            InitializeComponent();
        }

        private void InitGrid(BindingSource bs)
        {
            // 
            // dURATIONDataGridViewTextBoxColumn
            // 
            dURATIONDataGridViewTextBoxColumn.DataPropertyName = "DUR";
            dURATIONDataGridViewTextBoxColumn.HeaderText = "Длительность";
            dURATIONDataGridViewTextBoxColumn.Name = "dURATIONDataGridViewTextBoxColumn";
            dURATIONDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // cOSTDataGridViewTextBoxColumn
            // 
            cOSTDataGridViewTextBoxColumn.DataPropertyName = "COST";
            cOSTDataGridViewTextBoxColumn.HeaderText = "Стоимость";
            cOSTDataGridViewTextBoxColumn.Name = "cOSTDataGridViewTextBoxColumn";
            cOSTDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // cOSTDataGridViewTextBoxColumn
            // 
            NUMBERDataGridViewTextBoxColumn.DataPropertyName = "NUM";
            NUMBERDataGridViewTextBoxColumn.HeaderText = "Номер";
            NUMBERDataGridViewTextBoxColumn.Name = "NUMBERDataGridViewTextBoxColumn";
            NUMBERDataGridViewTextBoxColumn.ReadOnly = true;

            dataGridView1.MultiSelect = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
             NUMBERDataGridViewTextBoxColumn,
             dURATIONDataGridViewTextBoxColumn,
             cOSTDataGridViewTextBoxColumn});
            dataGridView1.DataSource = bs;
            dataGridView1.AutoResizeColumns();
        }


        /// <summary>Установка конпонентов пользовательской информации</summary>
        /// <param name="clientsBindingSource"></param>
        private void SetClientTextBoxes(DataRowView drv)
        {
            this.textBoxAddress.Text = drv["ADDRESS"].ToString();
            this.textBoxAgreement.Text = drv["ACCOUNT_NUMBER"].ToString();
            this.textBoxFIO.Text = drv["FIO"].ToString();
            this.textBoxPassport.Text = drv["PASSPORT"].ToString();
            //this.textBox1.Text = 
        }

        public bool ShowForm(DataRowView client, DataSetMain dataSet)
        {
            this.dataSet = dataSet;
            c_id = (decimal)client["CLIENT_ID"];
            acc_id = (decimal)client["ACCOUNT_NUMBER"];
            t_adapter.FillByClientId(dataSet.NUMBERS_STATS, c_id);
            t_adapter_draft.FillByClientId(dataSet.DRAFT_VIEW, c_id);
            bindingSource = new BindingSource(dataSet, "NUMBERS_STATS");
            bindingSourceDraft = new BindingSource(dataSet, "DRAFT_VIEW");
            InitGrid(bindingSource);
            SetClientTextBoxes(client);

            DataRowView drv = (DataRowView)bindingSourceDraft.Current;
            textBox1.Text = drv["ONE"].ToString();
            this.textBox2.Text = drv["TWO"].ToString();
            this.textBox3.Text = drv["THREE"].ToString();
            this.textBox4.Text = drv["FOUR"].ToString(); 


            if (ShowDialog() == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PreviewDraftForm f = new PreviewDraftForm(acc_id);
            f.Show();
            
        }


    }

    class PreviewDraftForm : Form
    {
        private PictureBox p_box;
        private Button btn = new Button();
        private Bitmap bmp;
        private decimal acc;
        
        public PreviewDraftForm(decimal num)
        {
            acc = num;
            bmp = iNet.GetImg("http://www.billing.int/index/getdraft/" + num + "/");
            if (bmp == null) MessageBox.Show("Ошибка генерации");
            p_box = new PictureBox();
            p_box.Image = bmp;
            p_box.Visible = true;

            p_box.Size = bmp.Size;
            this.Size = new Size(p_box.Width + 30, p_box.Height + 70);
            p_box.Location = new Point(10, 10);
            this.Controls.Add(p_box);

            btn.Text = "Сохранить";
            btn.Location = new Point(p_box.Width - 63, p_box.Height + 13);
            btn.Click += new EventHandler(btn_Click);
            this.Controls.Add(btn);

        }

        void btn_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.FileName = "c:\\" + acc + ".jpg"; //InitialDirectory = "C:\\";
            saveFileDialog.Filter = "JPEG images (*.jpg)|*.jpg";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                bmp.Save(saveFileDialog.FileName);
            }
        }


    }
}
